﻿var arrTask = new Array(); //总的小任务的数组
var taskInfo; //taskInfo.execstutas  0 未执行   1  已执行   2 只提示信息 success
var taskId = 0;
var executeTaskFlag = false;

//提示总任务的开始
function TaskInfoShowBegin(ac) {
    var taskmag = "";
    switch (ac) {
        case "NodePub":
            taskmag = "站点批量发布开始...";
            break;
        case "RePubNode":
            taskmag = "首页刷新开始...";
            break;
        case "NodeRefresh":
            taskmag = "站点批量更新开始...";
            break;
        case "repubcontent":
            taskmag = "批量更新开始...";
            break;
        case "pubcontent":
            taskmag = "批量发布开始...";
            break;
        default:
            taskmag = "执行操作开始执行...";
            break;
    }
    top.TaskInfoShow(taskmag, taskId);
}
//将总任务执行完毕的信息加到小任务中
function TaskInfoEndAdd(ac) {
    var taskmag = "";
    switch (ac) {
        case "NodePub":
            taskmag = "<font style='font-weight: bold'>[站点批量发布完成]</font>";
            break;
        case "RePubNode":
            taskmag = "<font style='font-weight: bold'>[首页刷新完成]</font>";
            break;
        case "NodeRefresh":
            taskmag = "<font style='font-weight: bold'>[结点更新完成]</font>";
            break;
        case "repubcontent":
            taskmag = "<font style='font-weight: bold'>[批量更新完成]</font>";
            break;
        case "pubcontent":
            taskmag = "<font style='font-weight: bold'>[批量发布完成]</font>";
            break;
        default:
            taskmag = "<font style='font-weight: bold'>[执行操作完成]</font>";
            break;
    }
    taskInfo = new Object();
    taskInfo.execstutas = 2;
    taskInfo.ac = ac;
    taskInfo.url_data = "";
    taskInfo.begin = "";
    taskInfo.success = taskmag;
    taskInfo.error = "";
    taskInfo.id = 0;
    arrTask.push(taskInfo);
}
function TaskInfoEndAdd(ac, NodeName) {
    var NodeNameStr = NodeName;
    if (NodeNameStr != "") {
        NodeNameStr = "(  " + NodeNameStr + ")";
    }
    var taskmag = "";
    switch (ac) {
        case "NodePub":
            taskmag = "<font style='font-weight: bold'>[站点批量发布完成]" + NodeNameStr + "</font>";
            break;
        case "RePubNode":
            taskmag = "<font style='font-weight: bold'>[首页刷新完成]" + NodeNameStr + "</font>";
            break;
        case "NodeRefresh":
            taskmag = "<font style='font-weight: bold'>[结点更新完成]" + NodeNameStr + "</font>";
            break;
        case "repubcontent":
            taskmag = "<font style='font-weight: bold'>[批量更新完成]" + NodeNameStr + "</font>";
            break;
        case "pubcontent":
            taskmag = "<font style='font-weight: bold'>[批量发布完成]" + NodeNameStr + "</font>";
            break;
        default:
            taskmag = "<font style='font-weight: bold'>[执行操作完成]" + NodeNameStr + "</font>";
            break;
    }
    taskInfo = new Object();
    taskInfo.execstutas = 2;
    taskInfo.ac = ac;
    taskInfo.url_data = "";
    taskInfo.begin = "";
    taskInfo.success = taskmag;
    taskInfo.error = "";
    taskInfo.id = 0;
    arrTask.push(taskInfo);
}
//添加单个的更新页面的任务
function AddContentPageTask(_groupIds, _beingIndex, _EndIndex, _nodename, _nideidId) {
    taskInfo = new Object();
    taskInfo.execstutas = 0;
    taskInfo.ac = "content";
    taskInfo.url_data = "&contentid=" + _groupIds + "&t=" + Math.random();
    taskInfo.begin = "开始更新内容页( id: " + _nideidId + ")   (" + _nodename + ") ...";
    taskInfo.success = "更新内容页( id:" + _nideidId + ")   (" + _nodename + ") 成功";
    taskInfo.error = "更新内容页(" + _beingIndex + " , " + _EndIndex + ")   (" + _nodename + ") 失败";
    //taskInfo.begin = "开始更新内容页(" + _beingIndex + " , " + _EndIndex + ")   (" + _nodename + ") ...";
    //taskInfo.success = "更新内容页(" + _beingIndex + " , " + _EndIndex + ")   (" + _nodename + ") 成功";
    //taskInfo.error = "更新内容页(" + _beingIndex + " , " + _EndIndex + ")   (" + _nodename + ") 失败";
    taskInfo.id = _nideidId;
    arrTask.push(taskInfo);
}
//添加更新首页的任务
function AddUpdateIndexPageTask(taskData, nodeid, nodename) {
    var CountNum = taskData.listpagecount;
    for (var i = 1; i <= CountNum; i++) {
        taskInfo = new Object();
        taskInfo.execstutas = 0;
        taskInfo.ac = "node";
        if (i == 1)
            taskInfo.show = 1;
        else
            taskInfo.show = 0;
        taskInfo.url_data = "&index=" + i.toString() + "&nodeid=" + nodeid + "&countNum=1" + "&t=" + Math.random(); ;
        taskInfo.begin = "开始更新[" + nodename + "]首页...";
        taskInfo.success = "更新[" + nodename + "]首页 页次:" + i.toString() + " 完成!";
        taskInfo.error = "更新[" + nodename + "]首页 页次:" + i.toString() + " 失败!";
        taskInfo.id = nodeid;
        arrTask.push(taskInfo);
    }
}
//添加附加发布的任务
function AddUpdateExtraPageTask(arrExtraIds, nodeid, nodename, listpagecount) {
    for (var k in arrExtraIds) {
        taskInfo = new Object();
        taskInfo.execstutas = 0;
        taskInfo.ac = "Extra";
        taskInfo.url_data = "&index=1&nodeid=" + nodeid + "&countNum=" + listpagecount + "&Extras=" + arrExtraIds[k] + "&t=" + Math.random();
        taskInfo.begin = "开始更新[" + nodename + "] (" + k + ")附加发布...";
        taskInfo.success = "更新[" + nodename + "] (" + k + ")附加发布 完成!";
        taskInfo.error = "更新[" + nodename + "] (" + k + ")附加发布失败!";
        taskInfo.id = nodeid;
        arrTask.push(taskInfo);
    }
}

function TaskInfoShowBeginForIframe(taskDataTemp) {
    if (taskDataTemp) {
        var ac = taskData.ac; // UrlParm.parm('ac');
        TaskInfoShowBegin(ac); //提示总任务的开始
    }
}
function bind() {
    for (var i = 0; i < taskDataArray.length; i++) {//总任务-begin
        taskData = taskDataArray[i];
        if (taskData.bind == 1)
            continue;
        if (taskData.bind == 0) {//分解一个总任务-begin
            taskData.bind = 1;
            if (taskData) {
                var ac = taskData.ac; // UrlParm.parm('ac');
                //TaskInfoShowBegin(ac); //提示总任务的开始
                var NodeNamesForTasks = "";
                if (taskData.items) {
                    var items = taskData.items;
                    var onceoper = taskData.onceoper; //单次处理页数
                    var c = 0;
                    for (var nodeid in items) {//循环结点数据
                        if (taskData.contentbln == "1") {//是否更新内容
                            if (items[nodeid].contents != "") {
                                var _contents = items[nodeid].contents;
                                var arrIds = _contents.split(',');
                                var _groupIds = "";
                                for (var k in arrIds) {
                                    _groupIds += arrIds[k] + ',';
                                    if ((c + 1) % onceoper == 0) {
                                        _groupIds = _groupIds.substring(0, _groupIds.length - 1);
                                        AddContentPageTask(_groupIds, c - onceoper + 1, c + 1, items[nodeid].nodename, nodeid);
                                        _groupIds = "";
                                    }
                                    c++;
                                }
                                if (_groupIds != "") {
                                    _groupIds = _groupIds.substring(0, _groupIds.length - 1);
                                    var _tasksBeginIndex = 0;
                                    var _tasksEndIndex = 0;

                                    if (c > onceoper) {
                                        _tasksBeginIndex = (c + 1) - onceoper;
                                        _tasksEndIndex = c + 1;
                                    } else {
                                        _tasksBeginIndex = 0;
                                        _tasksEndIndex = onceoper - 1;
                                    }
                                    AddContentPageTask(_groupIds, _tasksBeginIndex, _tasksEndIndex, items[nodeid].nodename, nodeid);
                                    _groupIds = "";
                                }
                            }
                        }
                        if (taskData.extrabln == "1") {//是否更新附加发布
                            if (items[nodeid].extras != "") {
                                AddUpdateExtraPageTask(items[nodeid].extras.split(','), nodeid, items[nodeid].nodename, taskData.listpagecount); //添加附加发布的任务
                            }
                        }
                        if (taskData.indexbln == "1") {//是否更新首页
                            AddUpdateIndexPageTask(taskData, nodeid, items[nodeid].nodename); //添加更新首页的任务    
                        }
                        if (NodeNamesForTasks.indexOf(items[nodeid].nodename) <= 0) {
                            NodeNamesForTasks += items[nodeid].nodename + "  ";
                        }

                    }

                    TaskInfoEndAdd(ac, NodeNamesForTasks); //将总任务执行完毕的信息加到小任务中

                }
            }
        } //分解一个总任务-end
    } //总任务-end
    BeginTask();

}
function BeginTask() { //开始任务
    // alert(executeTaskFlag);
    if (executeTaskFlag == false) {
        executeTaskFlag = true;
        Run();
    }
}

function Run() {

    if (taskId <= arrTask.length - 1) {
        var taskInfoTemp = arrTask[taskId];

        if (taskInfo && taskInfoTemp.execstutas == 0) {

            if (taskInfoTemp.ac != "node" || taskInfoTemp.show == 1)
                top.TaskInfoShow(taskInfoTemp.begin, taskId);
            $.ajax({
                type: "POST",
                url: "publish/admin_taskexec.aspx",
                data: 'ac=' + taskInfoTemp.ac + taskInfoTemp.url_data,
                async: true,
                error: function () {
                    taskInfoTemp.execstutas = 1;
                    top.TaskInfoShow(taskInfoTemp.error, taskId);
                    taskId = taskId + 1;
                    Run();
                },
                success: function (msg) {
                    //alert(msg);
                    taskInfoTemp.execstutas = 1;
                    try {
                        msg = eval("msg=" + msg);
                    }
                    catch (error)
                    { }
                    if (msg.result == "1") {
                        top.TaskInfoShow(taskInfoTemp.success, taskId);
                    }
                    else {
                        if (taskInfoTemp.ac == "node") {
                            for (var i = taskId; i <= arrTask.length - 1; i++) {
                                if (arrTask[i].ac == "node" && arrTask[i].id == taskInfoTemp.id) {
                                    arrTask[i].execstutas = 1;
                                }
                            }
                        }
                        if (taskInfoTemp.ac != "node" || taskInfoTemp.show == 1)
                            top.TaskInfoShow(taskInfoTemp.error, taskId);

                    }
                    taskId = taskId + 1;
                    Run();
                }
            });
        }
        else if (taskInfo && taskInfoTemp.execstutas == 1) {
            taskId = taskId + 1;
            Run();
        }
        else if (taskInfo && taskInfoTemp.execstutas == 2) {
            top.TaskInfoShow(taskInfoTemp.success, taskId);
            taskId = taskId + 1;
            Run();
        }
    }
    else {
        while (arrTask.pop()) { }
        taskId = 0;
        executeTaskFlag = false;
    }
}
function RunTask(ac, url_data) {
    var returntext = $.ajax({ type: "POST", url: 'admin_taskexec.aspx', data: 'ac=' + ac + url_data, async: true }).responseText;
    var msg = eval("msg=" + returntext);
    if (msg.result == "1") {
        return true;
    }
    else {
        return false;
    }
}